<!-- 承运商绑定司机 -->
<template>
  <el-dialog
    title="绑定司机"
    :visible.sync="dialogVisible"
    append-to-body
    width="60%"
  >
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @row-click="rowClick"
      @selection-change="selectionChange"
    >
      <!-- <template slot="radio" slot-scope="{ row }">
        <el-radio v-model="selectRow" :label="row.id">{{ "" }}</el-radio>
      </template> -->
    </avue-crud>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">关 闭</el-button>
      <el-button type="primary" v-noMoreClick @click="binding()">绑 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getList } from "@/api/basedata/driver";
import { bindOrUnBind } from "@/api/basedata/carriers";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        height: "auto",
        calcHeight: 200,
        tip: false,
        searchShow: false,
        searchMenuSpan: 6,
        border: true,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        menu: false,
        selection: true,
        labelWidth: "180",
        menuWidth: "250",
        column: [
          // {
          //   label: "单选",
          //   prop: "radio",
          //   width: 60,
          //   hide: false,
          //   showColumn: false,
          // },
          {
            label: "姓名",
            prop: "name",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入姓名",
                trigger: "blur",
              },
            ],
          },
          {
            label: "联系电话",
            prop: "phone",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入联系电话",
                trigger: "blur",
              },
            ],
          },
          {
            label: "从业资格证号",
            prop: "certificateWorkNo",
            rules: [
              {
                required: true,
                message: "请输入从业资格证号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "驾驶证号",
            prop: "certificateDriverNo",
            rules: [
              {
                required: true,
                message: "请输入驾驶证号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "准驾车型",
            prop: "canDriveCarType",
            rules: [
              {
                required: true,
                message: "请输入准驾车型",
                trigger: "blur",
              },
            ],
          },
          {
            label: "身份证有效期",
            type: "datetimerange",
            prop: "idCardValidDate",
            hide: true,
            showColumn: false,
            defaultTime: ["00:00:00", "23:59:59"],
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            startPlaceholder: "请选择开始日期",
            endPlaceholder: "请选择结束日期",
            rules: [
              {
                required: true,
                message: "请选择身份证有效期",
                trigger: "change",
              },
            ],
          },
          {
            label: "驾驶证有效期",
            defaultTime: ["00:00:00", "23:59:59"],
            type: "datetimerange",
            prop: "certificateDriverValidDate",
            hide: true,
            showColumn: false,
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            startPlaceholder: "请选择开始日期",
            endPlaceholder: "请选择结束日期",
            rules: [
              {
                required: true,
                message: "请选择驾驶证有效期",
                trigger: "change",
              },
            ],
          },
          {
            label: "备注",
            prop: "remark",
            hide: true,
            showColumn: false,
          },
          {
            label: "审核状态",
            prop: "approvalStatus",
            type: "select",
            editDisplay: false,
            addDisplay: false,
            props: {
              label: "value",
              value: "key",
            },
            // search: true,
            dicUrl: `/api/blade-tms/common/SelectEnum?name=approval`,
          },
          {
            label: "创建时间",
            type: "datetime",
            prop: "createTime",
            search: true,
            searchRange: true,
            editDisplay: false,
            addDisplay: false,
            hide: true,
            showColumn: false,
            defaultTime: ["00:00:00", "23:59:59"],
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            startPlaceholder: "请选择开始日期",
            endPlaceholder: "请选择结束日期",
          },
          {
            label: "身份证正面照片上传",
            prop: "idCardPicFront",
            hide: true,
            showColumn: false,
            type: "upload",
            span: 12,
            listType: "picture-img",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            rules: [
              {
                required: true,
                message: "请上传身份证正面照片",
                trigger: "change",
              },
            ],
          },
          {
            label: "身份证背面照片上传",
            prop: "idCardPicBack",
            hide: true,
            showColumn: false,
            type: "upload",
            span: 12,
            listType: "picture-img",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            rules: [
              {
                required: true,
                message: "请上传身份证背面照片",
                trigger: "change",
              },
            ],
          },
          {
            label: "驾驶证正页上传",
            prop: "certificateDriverPicFront",
            hide: true,
            showColumn: false,
            type: "upload",
            span: 12,
            listType: "picture-img",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            rules: [
              {
                required: true,
                message: "请上传驾驶证正页",
                trigger: "change",
              },
            ],
          },
          {
            label: "驾驶证副页上传",
            prop: "certificateDriverPicBack",
            hide: true,
            showColumn: false,
            type: "upload",
            span: 12,
            listType: "picture-img",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            rules: [
              {
                required: true,
                message: "请上传驾驶证副页",
                trigger: "change",
              },
            ],
          },
          {
            label: "从业资格证上传",
            prop: "certificateWorkPic",
            hide: true,
            showColumn: false,
            type: "upload",
            span: 12,
            listType: "picture-img",
            propsHttp: {
              res: "data",
              name: "originalName",
              url: "link",
            },
            action: "/api/blade-resource/oss/endpoint/put-file-attach",
            rules: [
              {
                required: true,
                message: "请上传从业资格证",
                trigger: "change",
              },
            ],
          },
        ],
      },
      data: [],
      selectRow: "", //选择数据
      selection: [], //选择数据
      dialogVisible: false,
      carrierId: "", //承运商id
    };
  },
  components: {},
  computed: {},
  methods: {
    searchReset() {
      this.query = {};
      this.getDriverList(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.getDriverList(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.getDriverList(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getDriverList(this.page);
    },
    refreshChange() {
      this.getDriverList(this.page, this.query);
    },
    open(id) {
      this.dialogVisible = true;
      // this.selectRow = "";
      this.selection = [];
      this.carrierId = id;
      this.getDriverList(this.page);
    },
    getDriverList(page, params = {}) {
      let query = Object.assign(params, this.query);
      if (query.createTime) {
        query = {
          ...query,
          start: query.createTime ? query.createTime[0] : "",
          end: query.createTime ? query.createTime[1] : "",
        };
        delete query.createTime;
      }
      this.loading = true;
      getList(page.currentPage, page.pageSize, {
        ...query,
        carrierId: this.carrierId,
      }).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.$nextTick(() => {
          this.$refs.crud.$refs.table.doLayout();
        });
      });
    },
    //单选
    rowClick(row) {
      // this.selectRow = row.id;
      this.$refs.crud.toggleRowSelection(row);
    },
    //多选
    selectionChange(list) {
      this.selection = list;
    },
    //绑定
    binding() {
      if (this.selection.length === 0) {
        this.$message({
          message: "请选择要绑定的司机",
          type: "warning",
        });
        return false;
      }
      bindOrUnBind({
        carrierId: this.carrierId,
        type: "driver",
        bindType: "bind",
        bindId: this.selection.map((item) => {
          return item.id;
        }),
      }).then(
        () => {
          this.dialogVisible = false;
          this.$emit("refresh");
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
        }
      );
    },
  },
};
</script>

<style></style>
